package com.earl.common.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@ConditionalOnProperty(name = "custom.swagger.enable", havingValue = "true")
public class BaseSwaggerConfig {
    @Value("${custom.swagger.basePackage:}")
    private String basePackage;
    @Value("${custom.swagger.title:}")
    private String title;
    @Value("${custom.swagger.description:}")
    private String description;
    @Value("${custom.swagger.serviceUrl:}")
    private String serviceUrl;
    @Value("${custom.swagger.version:}")
    private String version;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(title)
                .description(description)
                .termsOfServiceUrl(serviceUrl)
                .version(version)
                .build();
    }

}
